<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="queryMobile" placeholder="会员手机号" style="width: 200px; margin:5px 0 0 5px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="queryNickname" placeholder="会员昵称" style="width: 200px; margin:5px 0 0 5px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="queryAccount" placeholder="提现账号" style="width: 200px; margin:5px 0 0 5px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="queryWithdrawMobile" placeholder="提现人手机" style="width: 200px; margin:5px 0 0 5px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="queryName" placeholder="提现人姓名" style="width: 200px; margin:5px 0 0 5px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="queryType" placeholder="提现通道" clearable style="width: 150px; margin: 5px 0 0 5px" class="filter-item">
        <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item" />
      </el-select>
      <el-date-picker v-model="date" style="width: 260px; margin: 5px 0 0 5px" type="datetimerange" :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
      <el-select v-model="queryStats" placeholder="提现账号状态" clearable style="width: 150px; margin: 5px 0 5px 0" class="filter-item">
        <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item" />
      </el-select>
      <el-button style="margin:5px 0 5px 0" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" />
      <br>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column label="会员昵称" width="110">
        <template slot-scope="scope">
          {{ scope.row.member_nickname }}
        </template>
      </el-table-column>
      <el-table-column label="收款账号" width="auto" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.account }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款人姓名" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款人电话" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通道" width="120" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.type | typeCssFilter">
            {{ row.type | typeFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="120" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.stats | statusCssFilter">
            {{ row.stats | statusFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="currentPage" :limit.sync="pageSize" @pagination="fetchData({})" />

  </div>
</template>

<script>
import { getAccountList } from '@/api/account'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    typeFilter(type) {
      const typeMap = {
        2: '银行卡',
        1: '微信',
        0: '支付宝'
      }
      return typeMap[type]
    },
    typeCssFilter(type) {
      const typeMap = {
        2: 'warning',
        1: 'success',
        0: ''
      }
      return typeMap[type]
    },
    statusFilter(status) {
      const statusMap = {
        1: '启用',
        0: '禁用'
      }
      return statusMap[status]
    },
    statusCssFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      hidden: false,
      listLoading: true,
      // 翻页相关数据 当前页，每页显示条数，排序方式，排序字段，符合条件的搜索结果数，搜索结果
      currentPage: 1,
      pageSize: 10,
      sort: 'desc',
      order: 'id',
      total: 0,
      list: [],
      queryMobile: '',
      queryNickname: '',
      queryAccount: '',
      queryName: '',
      queryWithdrawMobile: '',
      queryType: '',
      queryStats: '',
      date: '',
      pickerOptions: {
        shortcuts: [{
          text: '昨天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近3天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      sortBy: '',
      // 搜索条件相关数据
      typeOptions: ['支付宝', '微信', '银行卡'],
      statusOptions: ['禁用', '启用'],
      // 下载的loading
      downloadLoading: false,
      downloadData: []
    }
  },
  mounted() {},
  created() {
    this.fetchData()
  },
  methods: {
    handleFilter() {
      this.currentPage = 1
      this.fetchData()
    },
    formatJson(filterVal) {
    },
    fetchData() {
      const param = {
        sort: this.sort,
        order: this.order,
        page: this.currentPage,
        page_size: this.pageSize
      }
      if (this.queryMobile !== '') {
        param.mobile = this.queryMobile
      }
      if (this.queryNickname !== '') {
        param.nickname = this.queryNickname
      }
      if (this.queryMobile !== '') {
        param.mobile = this.queryMobile
      }
      if (this.queryAccount !== '') {
        param.account = this.queryAccount
      }
      if (this.queryName !== '') {
        param.name = this.queryName
      }
      if (this.queryWithdrawMobile !== '') {
        param.withdraw_mobile = this.queryWithdrawMobile
      }
      if (this.queryType !== '') {
        this.typeOptions.map((item, idx) => {
          if (this.queryType === item) {
            param.type = idx
          }
        })
      }
      if (this.queryStats !== '') {
        this.statusOptions.map((item, idx) => {
          if (this.queryStats === item) {
            param.stats = idx
          }
        })
      }
      if (this.date !== '' && this.date !== null) {
        console.log(this.date)
        param.start_created_at = this.date[0].toLocaleDateString() + ' ' + this.date[0].toTimeString().split(' ')[0]
        param.end_created_at = this.date[1].toLocaleDateString() + ' ' + this.date[1].toTimeString().split(' ')[0]
      }
      console.log(param)
      getAccountList(param).then(response => {
        const { data, meta } = response
        this.list = data
        this.listLoading = false
        this.currentPage = parseInt(meta.current_page)
        this.total = parseInt(meta.total)
      }).catch(() => {
        this.listLoading = false
        this.list = []
      })
    }
  }
}
</script>
<style scoped>
  .filter-container {
    background: #fff;
    padding: 32px 0px;
  }
</style>
